home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / ex / ex6-2.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  4KB  |  151 lines

  1. // ex6-2.c -- Improved geometry class hierarchy
  2.  
  3. // $Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/ex/RCS/ex6-2.c,v 3.0 90/05/15 22:45:42 kgorlen Rel $
  4.  
  5. #include <iostream.h>
  6. #include <osfcn.h>
  7.  
  8. class Point {
  9.     int xc,yc;      // x-y coordinates
  10. public:
  11.     Point()                     { xc = yc = 0; }
  12.     Point(int newx, int newy)   { xc=newx; yc=newy; }
  13.     int x() const               { return xc; }
  14.     int x(int newx)             { return xc = newx; }
  15.     int y() const               { return yc; }
  16.     int y(int newy)             { return yc = newy; }
  17.     Point operator+(const Point& p) const {
  18.         return Point(xc+p.xc, yc+p.yc);
  19.     }
  20.     void operator+=(const Point& p) {
  21.         xc += p.x();
  22.         yc += p.y();
  23.     }
  24.     void printOn(ostream& strm=cout) const;
  25. };
  26.  
  27. void Point::printOn(ostream& strm) const
  28. {
  29.     strm << '(' << xc << ',' << yc << ')';
  30. }
  31.  
  32. ostream& operator<<(ostream& strm, const Point& p) 
  33. {
  34.     p.printOn(strm);
  35.     return strm;
  36. }
  37.  
  38. class TransformStack {
  39.     Point s[100];   // array to hold stack of points
  40.     Point* top;     // current top of stack
  41. public:
  42.     TransformStack()        { top = s; }
  43.     Point* current() const  { return top; }
  44.     void push(Point& p) {
  45.         Point newtop = *top + p;
  46.         *++top = newtop;
  47.     }
  48.     void pop()              { top--; }
  49. };
  50.  
  51. TransformStack transform;   // shape translation stack
  52.  
  53. class Shape {
  54.     Point org;      // origin
  55. protected:
  56.     Shape(const Point& p) : org(p)  {}
  57. public:
  58.     Point origin() const                { return org; }
  59.     virtual void move(const Point& d)   { org += d; }
  60.     virtual void draw() const =0;
  61. };
  62.  
  63. class Line: public Shape {
  64.     Point p;        // end point
  65. public:
  66.     Line(const Point& a, const Point& b) : Shape(a), p(b) {}
  67.     virtual void move(const Point&);
  68.     virtual void draw() const;
  69. };
  70.  
  71. void Line::move(const Point& d)
  72. {
  73.     Shape::move(d);
  74.     p += d;
  75. }
  76.  
  77. void Line::draw() const
  78. {
  79.     cout << "Line from " << *transform.current() + origin()
  80.         << " to " << *transform.current() + p << endl;
  81. }
  82.  
  83. class Circle: public Shape {
  84.     int rad;        // radius of circle
  85. public:
  86.     Circle(const Point& c, int r) : Shape(c) { rad = r; }
  87.     virtual void draw() const;
  88. };
  89.  
  90. void Circle::draw() const
  91. {
  92.     cout << "Circle with center "
  93.          <<  *transform.current() + origin()
  94.          << " and radius " << rad << endl;
  95. }
  96.  
  97. const unsigned PICTURE_CAPACITY = 100;
  98.  
  99. class Picture : public Shape {
  100.     Shape* s[PICTURE_CAPACITY]; // array of pointers to shapes
  101.     int n;                      // number of shapes in this Picture
  102. public:
  103.     Picture() : Shape(Point(0,0))    { n = 0; } // constructor
  104.     Picture(Point& org) : Shape(org) { n = 0; } // constructor
  105.     void add(Shape&);                       // add Shape to Picture
  106.     virtual void draw() const;              // draw picture;
  107. };
  108.  
  109. void Picture::add(Shape& t)
  110. {
  111.     if (n == PICTURE_CAPACITY) {
  112.         cerr << "Picture capacity exceeded\n";
  113.         exit(1);
  114.     }
  115.     s[n++] = &t;        // add pointer to Shape to Picture
  116. }
  117.  
  118. void Picture::draw() const  // draw a Picture
  119. {
  120.     transform.push(origin());
  121.     for (int i=0; i<n; i++) s[i]->draw();
  122.     transform.pop();
  123. }
  124.  
  125. main()
  126. {
  127.     Line l(Point(1,2),Point(3,4));  // create a Line
  128.     Circle c(Point(5,6),1);         // create a Circle
  129.     l.draw();                       // draw the line
  130.     c.draw();                       // draw the circle
  131.     Picture p;                      // create an empty Picture
  132.     l.move(Point(1,0));             // move the line
  133.     p.add(l);                       // add the line to the picture
  134.     c.move(Point(1,0));             // move the circle
  135.     p.add(c);                       // add it to the picture
  136.     p.draw();                       // draw the picture
  137.     p.move(Point(10,10));           // translate it by (10,10)
  138.     p.draw();                       // draw it again
  139.  
  140.     Picture bigPic;
  141.     Picture littlePic1, littlePic2(*new Point(10,10));
  142.     littlePic1.add(*new Line(Point(1,1),Point(2,2)));
  143.     littlePic1.add(*new Circle(Point(3,3),1));
  144.     littlePic2.add(*new Line(Point(4,4),Point(5,5)));
  145.     littlePic2.add(*new Circle(Point(3,3),2));
  146.     littlePic2.move(Point(1,1));
  147.     bigPic.add(littlePic1);
  148.     bigPic.add(littlePic2);
  149.     bigPic.draw();
  150. }
  151.